package com.lifeonwalden.dsg.service;

import com.lifeonwalden.dsg.bean.DateGenerateRule;
import com.lifeonwalden.dsg.constant.HolidayAdjustRule;

import java.util.Date;
import java.util.List;

/**
 * 内部工具类
 * <p>
 * 请勿直接使用
 */
public interface InnerUtil {
    static Date adjustForHoliday(HolidayAdjustRule adjustRule, Date date, DateService dateService) {
        Date result = date;
        switch (adjustRule) {
            case MOVE_TO_NEXT: {
                result = dateService.fetchNonHolidayOrNext(date);
                break;
            }
            case MOVE_TO_PREVIOUS: {
                result = dateService.fetchNonHolidayOrPrevious(date);
                break;
            }
        }

        return result;
    }

    static boolean notExistWhenAdjusted(List<Date> dateList, Date date) {
        return dateList.isEmpty() || dateList.get(dateList.size() - 1).compareTo(date) != 0;
    }

    static void acceptValidDate(List<Date> dateList, Date date, DateGenerateRule rule, DateService dateService) {
        Date nonHoliday = InnerUtil.adjustForHoliday(rule.getHolidayAdjustRule(), date, dateService);
        if (nonHoliday.compareTo(rule.getStart()) >= 0 && nonHoliday.compareTo(rule.getEnd()) <= 0 && InnerUtil.notExistWhenAdjusted(dateList, nonHoliday)) {
            dateList.add(nonHoliday);
        }
    }
}